package com.example.nonggo.model;

import android.media.MediaPlayer;
import android.util.Log;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.example.nonggo.Constants;
import com.example.nonggo.api.BaseApi;
import com.example.nonggo.api.MDataObserver;
import com.example.nonggo.bean.BdsoundDTO;
import com.example.nonggo.bean.BdsoundVO;
import com.example.nonggo.bean.PresetQuestionRes;
import com.example.nonggo.contract.HomeContract;
import com.example.nonggo.utils.FileUtils;
import com.tencent.mmkv.MMKV;

import java.io.File;

public class HomeModel extends BaseModel<HomeContract.Presenter>{


    public void getQuestion(){
        RxHttpUtils.
                createApi(BaseApi.class).getPresetQuestions("Bearer "+token,"1","5")
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<PresetQuestionRes>(){

                    @Override
                    protected void onSuccess(PresetQuestionRes data) {
                        callback.callBackQuestion(data);
                    }
                });
    }


    public void fileTranslateText(String absolutePath) {
        Log.e("ExpertQaActivity", "transcribeAudio: " + absolutePath);
//        MediaPlayer player = new MediaPlayer();
//        player.
        String s = FileUtils.fileToBase64(absolutePath);
        File convertedFile = new File(absolutePath);
        String token = MMKV.defaultMMKV().getString(Constants.ACCESS_TOKEN, "0");
        RxHttpUtils.createApi(BaseApi.class)
                .bdsound(token, new BdsoundDTO(s, convertedFile.length()).toRequestBody())
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<>() {
                    @Override
                    protected void onSuccess(BdsoundVO data) {
                        if (data.getResult() == null) {
                            return;
                        }
                        callback.translateText(data.getResult()[0]);
                    }

                    @Override
                    public void doOnCompleted() {
                        super.doOnCompleted();
                        try {
                            // 根据路径删除文件
                            if (convertedFile.exists()) {
                                convertedFile.delete();
                            }
                        } catch (Exception ignore) {

                        }
                    }
                });
    }
}
